let axios = require('axios')
let qs = require('qs')

class Chrome {

  constructor(input){
    this.config = Object.assign({
      headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36'
      },
      data: {

      }
    })
  }

  get(url, data, header, urlData){
    return new Promise(resolve => {

      if(url.indexOf('?') === -1){
        url += '?' + ( urlData ? decodeURIComponent(qs.stringify(urlData)) : '')
      }

      axios({
        method: 'GET',
        url: url,
        data: Object.assign(this.config.data, data || {}),
        headers: Object.assign(this.config.headers, header)
      }).then(function (response) {
        // handle success
        resolve({
          code: 200,
          data: response.data,
          response
        })
      })
        .catch(function (error) {
          // handle error
          resolve({
            code: 500,
            data: null,
            error
          })
        })

    })
  }


  post(url, data, header){
    return new Promise(resolve => {

      axios({
        method: 'POST',
        url: url,
        data: Object.assign(this.config.data, data || {}),
        headers: Object.assign(this.config.headers, header)
      }).then(function (response) {
        // handle success
        resolve({
          code: 200,
          data: response.data,
          response
        })
      })
        .catch(function (error) {
          // handle error
          resolve({
            code: 500,
            data: null,
            error
          })
        })

    })
  }



  concatHubData(json){
    return `${json.hub}/${json.project}/raw/master/${json.path}`
  }

}



module.exports = new Chrome()
